package cn.summit.service.impl;

import cn.summit.service.CustmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.stereotype.Service;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author summit
 * @since 2020/2/28 22:09
 */
@Service("mySuccessHandler")
public class MySuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {

    @Autowired
    private CustmService custmService;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws ServletException, IOException {

        int test = custmService.getTest();
        if (authentication.getName().equals("summit")) {
            // todo
        }

        //authentication.
        String url;
        if (test == 0) {
            url = "/index";
        } else {
            url = "/t1/add";
        }
        // response.sendRedirect(request.getContextPath() + url);
        // 会帮我们跳转到上一次请求的页面上
        super.onAuthenticationSuccess(request, response, authentication);
    }
}
